<?php
class App_Comments extends Data_Base
{
    public function __construct ($type, $nid)
    {
        $this->_type = $type;
        $this->_nid = $nid;
    }

    public function uidListByCommented ()
    {
        $query = DB::select(DB::expr('distinct(`uid`) as uid'))
            ->from('user_v2_comments')
            ->where('type', '=', $this->_type)
            ->where('nid', '=', $this->_nid)
            ->execute();

        $uids = array();
        foreach ($query as $rs)
            $uids[] = $rs['uid'];

        return $uids;
    }

    public function add ($user, $message, $toId = 0, $toUid = 0)
    {
        if (! preg_match('/^@.+:/', $message))
        {
            $toId = 0;
            $toUid = 0;
        }

        $message = htmlentities($message);

        $row = array(
            'type' => $this->_type,
            'nid'  => $this->_nid,
            'uid'  => $user->getUserUid(),
            'toId' => $toId,
            'toUid' => $toUid,
            'message' => $message,
            'created' => DB::expr('unix_timestamp(now())'),
        );

        list($insert_id, $rows) = DB::insert('user_v2_comments')
            ->columns (array_keys($row))
            ->values (array_values($row))
            ->execute();

        if ($insert_id > 0)
        {
            Kohana::$log->add(Log::DEBUG, "comment:" . $this->_type ."/".$this->_nid. "/incCounts");
            App_Item::factory($this->_type, $this->_nid)->incCounts('countsComment');
            $user->incCounts('countsComment');

            if ($toUid > 0)
            {
                $this->addPushAppMessage($this->_type, $this->_nid, $toUid, $user->getUserUid(), $message);
            }
        }

        return $insert_id;
    }

    private function addPushAppMessage($type, $nid, $toUid, $fromUid, $message)
    {
        if ($type == 'goods')
        {
            $goods = App_Goods::info($nid);
            $image = $goods['thumb'];
            $url   = $goods['url'];
        }
        else
        {
            $goods = App_Gonglue::info($nid);
            $image = $goods['image'];
            $url   = $goods['url'];
        }


        App_Message::record($toUid, $fromUid, $message, $type, $nid, $image, $url);
        $title = "回复了你";
        $activityName = 'ArticleActivity';
        $alias = array('u'.$toUid);
        $push = new JPush;
        $push->alert_2_aliaslist($alias, $title, $message, $url, $activityName);
    }

    public static function info ($nid)
    {
        
        $detail = self::item('tiyu_gonglue', 'id', $nid);
        if (empty($detail))
            return array();

        return array(
            'name' => $detail['name'],
            'id'   => $detail['id'],
            'image' => $detail['image'],
            'url'    => 'http://'.$_SERVER['HTTP_HOST'].'/m/gonglue/detail/'.$nid,
        );
    }

    public static function items ($type, $nid, $offset, $limit, $newer_by_comment_nid = NULL)
    {
        $counts = self::counts('user_v2_comments', array(
            array('type', '=', $type),
            array('nid', '=', $nid),
            array('toId', '=', 0),
        ));

        $query = DB::select('c.*', 'u.userName', 'u.userIcon')
            ->from(array('user_v2_comments', 'c'))
            ->join(array('user_v2', 'u'), 'LEFT')
            ->on('c.uid', '=', 'u.uid')
            ->where('type', '=', $type)
            ->where('nid', '=', $nid)
            ->offset($offset)
            ->limit($limit)
            ->order_by('created', 'desc');

        if ($newer_by_comment_nid > 0)
            $query->where('c.id', '>=', $newer_by_comment_nid);
                
        $query = $query->execute();

        $list = array();
        foreach ($query as $rs)
        {
            $rs['date'] = date("Y-m-d H:i", $rs['created']);
            $list[] = $rs;
        }

        return array($counts, $list);
    }

}
